//
// Created by lyc on 2024/5/26.
//
#include"C:\Users\lyc\Desktop\2\2.2\OOP\OOP_experiment\experiment_3\ProductManagement.cpp"

class purchaseRecords{
private:
    int choice_number;
    int purchase_number;
public:
    purchaseRecords(int cn=0,int pn=0){
        choice_number = cn;
        purchase_number = pn;
    }
    void addRecord();
    void queryRecords();
};


void purchaseRecords::addRecord() {
    fstream file(R"(C:\Users\lyc\Desktop\2\2.2\OOP\OOP_experiment\experiment_3\shoppingCart.txt)");
    string line,product;
    bool findUser=false;
    while(getline(file,line)){
        if(line=="User: "+CURRENT_LOGIN_NAME){
            getline(file,line);
            getline(file,product);
            for(int i=1;i<choice_number;i++){
                getline(file,line);
                getline(file,product);
            }
        }
    }
    file.close();

go:
    fstream anotherFile(R"(C:\Users\lyc\Desktop\2\2.2\OOP\OOP_experiment\experiment_5\purchaseRecords.txt)");
    string newline;
    vector<string>lines;
    while(getline(anotherFile,newline)){
        if(newline=="User: "+CURRENT_LOGIN_NAME){
            lines.push_back(newline);
            findUser=true;
//            anotherFile<<line<<endl<<"number:"<<purchase_number<<endl<<"time:";

            lines.push_back(product);
            line ="number:"+ to_string(purchase_number);
            lines.push_back(line);
            time_t now;
            time(&now);
            struct tm* timeinfo;
            timeinfo = localtime(&now);
            char buffer[80];
            strftime(buffer,80,"%Y-%m-%d-%H:%M:%S",timeinfo);
//            anotherFile<<buffer<<endl
            lines.push_back(buffer);
            getline(anotherFile,newline);
        }
        lines.push_back(newline);
    }
    anotherFile.close();

    fstream addRecords(R"(C:\Users\lyc\Desktop\2\2.2\OOP\OOP_experiment\experiment_5\purchaseRecords.txt)");
    for(string newLine:lines){
        addRecords<<newLine<<endl;
    }
    addRecords.close();

    if(findUser==false){
        fstream newFile(R"(C:\Users\lyc\Desktop\2\2.2\OOP\OOP_experiment\experiment_5\purchaseRecords.txt)",ios::app);
        newFile<<"User: "+CURRENT_LOGIN_NAME<<endl<<"end"<<endl;
        newFile.close();
        goto go;
    }
}

void purchaseRecords::queryRecords() {
    fstream file(R"(C:\Users\lyc\Desktop\2\2.2\OOP\OOP_experiment\experiment_5\purchaseRecords.txt)");
    string line;
    while(getline(file,line)){
        if(line=="User: "+CURRENT_LOGIN_NAME){
            cout<<line<<endl;
            while(line!="end"){
                getline(file,line);
                cout<<line<<endl;
            }
        }
    }
    file.close();
}

//int main(){
//    CURRENT_LOGIN_NAME = "2203051";
//    purchaseRecords PR(1,5);
//    PR.addRecord();
//    PR.queryRecords();
//    return 0;
//}